import { Component, ChangeDetectorRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { LoginPage } from '../login/login';
import { HttpClient } from '@angular/common/http';
import {ip} from '../../providers/ip/ip'
import { ToastController } from 'ionic-angular';
// import { InjectionDependencyProvider } from '../../../providers/injection-dependency/injection-dependency'

/**
 * Generated class for the RegisterPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
	selector: 'page-register',
	templateUrl: 'register.html'
})
export class RegisterPage {
	jishi: any;
	time: any;
	submitbtn;
	codebtn;
	data:any;
	sumbitbtnfontsize;
	codebtnfontsize;
	registerData = {
		phone: '',
		password: '',
		name: '',
		icon: '1',
		verificationCode: ''
	};
	verifyCode: any = {
		verifyCodeTips: '获取验证码',
		countdown: 60,
		disable: true
	};

	constructor(
		public cd: ChangeDetectorRef,
		private http: HttpClient,
		public navCtrl: NavController,
		public navParams: NavParams,
		private toastCtrl: ToastController
	) {
		this.time = 60;
	}

	tologin() {
		this.navCtrl.push(LoginPage);
	}
	registerUser() {
		if (
			this.registerData.phone == '' ||
			!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(this.registerData.phone)
		) {
			this.presentToast('请输入正确格式手机号码')

			return;
		}
		//判断密码是否为空
		if (this.registerData.password == '') {
			this.presentToast('请输入密码')

			return;
		}
		//判断密码是否包含且只有数字和字母且大于等于6位并且小于等于16位
		if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(this.registerData.password)) {
			this.presentToast('密码必须由大于5位小于17位数字和字母构成');

			return;
		}
		if (this.registerData.verificationCode == '') {
			this.presentToast('请输入验证码');

			return;
		}
		console.log(this.registerData);
		let that = this;
		this.http
			.post(
				ip+'api/register',
				{
					phone: that.registerData.phone,
					password: that.registerData.password,
					code: that.registerData.verificationCode
				},
				{}
			)
			.subscribe(
				function(data) {
					// console.log(data);
					// alert(data.data.token);
					// localStorage.setItem('token', data.data.token);
					// console.log(localStorage.getItem('token'));
					// that.navCtrl.push(TabsPage);
					console.log(data);
					that.data=data;
					that.presentToast(that.data.msg);
				},
				function(err) {
					console.log(err);
				}
			);
	}
	getCode() {
		if (this.registerData.phone == '') {
			// this.Injection.Native.openToast('请输入手机号')
			this.presentToast('请输入手机号');
			return;
		}
		//检验手机号码格式
		if (!/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(this.registerData.phone)) {
			// this.Injection.Native.openToast('请输入正确格式手机号码')
			// alert('请输入正确格式手机号码');
			this.presentToast('请输入正确格式手机号码');
			return;
		}
		if(this.verifyCode.countdown!=60){
			return;
		}
		let that = this;
		console.log(ip);
		this.http.post(ip+'api/yan', { phone: that.registerData.phone }).subscribe(
			function(data) {
				console.log(data);
				that.data=data;
				that.presentToast(that.data.msg);
			},
			function(err) {
				console.log(err);
			}
		);
		this.settime();
	}
	settime() {
		//发送验证码时间60秒重置
		if (this.verifyCode.countdown == 1) {
			this.verifyCode.countdown = 60;
			this.verifyCode.verifyCodeTips = '获取验证码';
			this.verifyCode.disable = true;
			return;
		} else {
			this.verifyCode.countdown--;
		}
		this.verifyCode.verifyCodeTips = '重新获取(' + this.verifyCode.countdown + ')';
		setTimeout(() => {
			this.verifyCode.verifyCodeTips = '重新获取(' + this.verifyCode.countdown + ')';
			this.settime();
		}, 1000);
	}

	presentToast(e) {
		let toast = this.toastCtrl.create({
			message: e,
			duration: 3000, //3秒后自动消失
			position: 'middle', //位置
			// showCloseButton: true,
			// closeButtonText: '关闭',
			dismissOnPageChange:false,
			cssClass: 'changeToast'
		});
		toast.onDidDismiss(() => {
			console.log('toast被关闭之后执行');
		});
		toast.present(); //符合触发条件后立即执行显示。一定不能忘了这个
	}
}
